//
//  DSXScoreboardViewController.m
//  DerbyOSSampler
//
//  Created by glenn andreas on 3/6/14.
//  Copyright (c) 2014 glenn andreas. All rights reserved.
//

#import "DSXScoreboardViewController.h"
#import "DerbyStopwatch.h"
#import "DerbyBoutState.h"
#import "DerbyNSOde.h"
@interface DSXScoreboardViewController ()
@property (strong, nonatomic) DerbyBoutState *oldBoutState;
@end

@implementation DSXScoreboardViewController
- (void) dealloc
{
    [self removeObserver:self forKeyPath:@"boutState.boutState"];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (void) loadView
{
    self.jamTimer = [[DerbyStopwatch alloc] initWithMaximumTime:120 countsUp:NO];
    self.periodTimer = [[DerbyStopwatch alloc] initWithMaximumTime:60 * 30 countsUp:NO];
    self.lineupTimer = [[DerbyStopwatch alloc] initWithMaximumTime:30 countsUp:YES];
    self.timeoutTimer = [[DerbyStopwatch alloc] initWithMaximumTime:60 countsUp:YES];
    self.intermissionTimer = [[DerbyStopwatch alloc] initWithMaximumTime:12 * 60 * 60 countsUp:NO];
    self.boutState = self.nsode.boutState;
    [self.boutState addObserver:self forKeyPath:@"boutState" options:0 context:0];
    // let state manage the timers
    self.boutState.timeoutTimer = self.timeoutTimer;
    self.boutState.jamTimer = self.jamTimer;
    self.boutState.periodTimer = self.periodTimer;
    self.boutState.lineupTimer = self.lineupTimer;
    self.boutState.intermissionTimer = self.intermissionTimer;
    self.boutState.timeoutTimer = self.timeoutTimer;
    self.oldBoutState = [self.boutState copy];
    [super loadView];
}

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"boutState"]) {
        if (self.boutState.boutState == self.oldBoutState.boutState)
            return; // didn't change

        self.oldBoutState = [self.boutState copy];
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
@end
